//
//  RequestData.m
//  Activity
//
//  Created by 李佳骏 on 15/6/30.
//  Copyright (c) 2015年 J.Lee. All rights reserved.
//

#import "RequestData.h"

@implementation RequestData

- (void)requestWithURL:(NSString *)url {
    //1. 创建URL对象
    NSURL *reqUrl = [NSURL URLWithString:url];
    //2. 创建request对象
    NSURLRequest *request = [NSURLRequest requestWithURL:reqUrl];
    //3. 发送异步请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            //4. 传值
        if (data) {
            [_delegate requestSuccess:data];
        } else {
            [_delegate requestError:connectionError];
        }
    }];
}

- (NSString *)creatFolderWithPath:(NSString *)path {
    NSArray *cachArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachPath = [cachArray lastObject];
    NSString *folderPath = [cachPath stringByAppendingPathComponent:path];
    NSLog(@"%@", folderPath);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:folderPath]) {
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return folderPath;
}

@end
